package cn.zlj.pethome.org.controller;
import cn.zlj.pethome.basic.QueryResult;
import cn.zlj.pethome.basic.annotation.LogAnno;
import cn.zlj.pethome.basic.annotation.PreAuthorize;
import cn.zlj.pethome.basic.util.AjaxResult;
import cn.zlj.pethome.org.domain.TDepartment;
import cn.zlj.pethome.org.query.QueryPage;
import cn.zlj.pethome.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@LogAnno
public class TDepartmentController {
    @Autowired
    private  DepartmentService departmentService;

   // @PreAuthorize(name = "查询部门树",sn = "department:tree")
    @GetMapping("/tree")
    public List<TDepartment> tree(){
        return departmentService.getList();
    }


    @PutMapping
    public AjaxResult save(@RequestBody TDepartment tDepartment){
        if(tDepartment.getId()==null){
            departmentService.save(tDepartment);
        }else {
            departmentService.update(tDepartment);
        }
        return  AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        if(id==null){
        throw new RuntimeException();
        }
       departmentService.del(id);
        return  AjaxResult.me().success();
    }
    @GetMapping
    public List<TDepartment> list(){
       return departmentService.getList();
    }
    @GetMapping("/{id}")
  //  @PreAuthorize(name = "根据id查询部门",sn = "department:findById")
    public TDepartment getOne(@PathVariable("id") Long id){
        return departmentService.getOne(id);
    }
    @PostMapping
    @PreAuthorize(name = "分页查询部门",sn = "department:list")
    public QueryResult<TDepartment> queryByPage(@RequestBody QueryPage page){
        return departmentService.queryPage(page);
    }
    @PatchMapping
   // @PreAuthorize(name = "批量删除部门",sn = "department:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        departmentService.batchDel(ids);
        return AjaxResult.me().success();
    }

}
